package vn.com.shoppie.object;

import java.util.ArrayList;

import vn.com.shoppie.activity.R;
import vn.com.shoppie.sobject.Campaign;
import vn.com.shoppie.sobject.ShoppieObject;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainAdapter extends BaseAdapter {
	LayoutInflater inflater;
	private Context ctx;
	int imageBackground;
	ArrayList<ShoppieObject> mShops;

	public MainAdapter(Context c, ArrayList<ShoppieObject> objects) {
		ctx = c;
		this.mShops = objects;
		inflater = (LayoutInflater) c
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		TypedArray ta = c.obtainStyledAttributes(R.styleable.Gallery1);
		imageBackground = ta.getResourceId(
				R.styleable.Gallery1_android_galleryItemBackground, 1);
		ta.recycle();
	}

	@Override
	public int getCount() {

		return mShops.size();
	}

	@Override
	public Object getItem(int arg0) {

		return arg0;
	}

	@Override
	public long getItemId(int arg0) {

		return arg0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			ItemShopHolder holder = new ItemShopHolder();
			v = LayoutInflater.from(ctx).inflate(R.layout.item_shop, parent,
					false);
			holder.lyAvatar = (LinearLayout) v.findViewById(R.id.ly_avatar);

			holder.avatar = new ImageView(ctx);
			holder.avatar.setImageResource(R.drawable.images);
			holder.avatar.setScaleType(ImageView.ScaleType.FIT_XY);
			holder.avatar.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			// holder.avatar.setBackgroundResource(imageBackground);
			holder.lyAvatar.addView(holder.avatar);

			holder.title = (TextView) v.findViewById(R.id.tv_title);
			holder.desc = (TextView) v.findViewById(R.id.tv_desc);

			v.setTag(holder);
		}
		ItemShopHolder holder = (ItemShopHolder) v.getTag();
		fillShopToView(holder, mShops.get(position));

		return v;
	}

	private void fillShopToView(final ItemShopHolder holder, ShoppieObject item1) {
		if (item1 instanceof Campaign) {
			final Campaign data = (Campaign) item1;
			new AsyncTask<String, Integer, Bitmap>() {

				@Override
				protected Bitmap doInBackground(String... arg0) {
					Bitmap bmp=(data.getBitmap(ctx, (data.linkImage)));
					if (bmp == null) {
						String link = data.linkImage;
						link = link.replaceAll(" ", "%20");
						bmp=data.getBitmap(ctx, data.linkImage);
					}
					return bmp;
				}
				protected void onPostExecute(Bitmap result) {
					if(result!=null){
						holder.avatar.setImageBitmap(result);
					}
				};
			}.execute(data.linkImage);
			
			holder.title.setText(data.name);
			holder.desc.setText(data.desc);
		}
	}

	class ItemShopHolder {
		LinearLayout lyAvatar;
		ImageView avatar;
		TextView title;
		TextView desc;
	}
}